package com.hyc.service;

import com.hyc.dao.NewsMapper;
import com.hyc.entity.News;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NewsService {
    @Autowired
    private NewsMapper newsMapper;

    public List<News> queryList(News con) {
        return newsMapper.queryList(con);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int update(News entity) {
        return newsMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteByIdList(List<Long> ids) {
        return newsMapper.deleteByIdList(ids);
    }

    public News findById(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    public void addOrUpdate(News entity) {
        if(StringUtils.isBlank(entity.getNewsAuthor())){
            entity.setNewsAuthor("admin");
        }
        if(entity.getId() == null){
            newsMapper.insertSelective(entity);
        } else {
            newsMapper.updateByPrimaryKeySelective(entity);
        }
    }
}
